<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancel" :visible.sync="dialog"
             :title="isAdd ? '新增' : '关联经销商'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="用户ID">
        <el-input v-model="form.uid" :disabled="true" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="用户昵称">
        <el-input v-model="form.nickname" :disabled="true" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="经销商：" prop="distributorId">
        <el-autocomplete v-model="form.distributorName" placeholder="请选择分销商ID" :trigger-on-focus="true"
                         :fetch-suggestions="queryDistributorSearchAsync" @select="handleDistributorSelect">
        </el-autocomplete>
      </el-form-item>
      <!--  <el-col v-bind="grid2">
          <el-form-item label="分销商ID">
            <el-input v-model="form.distributorId"  />
          </el-form-item>
        </el-col>
        <el-col v-bind="grid2">
          <el-form-item label="分销商名称">
            <el-input v-model="form.distributorName" />
          </el-form-item>
        </el-col>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
      </div>
  </el-dialog>
</template>

<script>
  import {add, edit, editp, addDistributor} from '@/api/yxUser';
  import {getDistributorInfo} from '@/api/yxStoreOrder';

  export default {
    props: {
      isAdd: {
        type: Boolean,
        required: true
      }
    },
    data() {
      return {
        loading: false, dialog: false,
        form: {
          uid: '',
          nickname: '',
          ptype: '1',
          distributorId: '',
          distributorName: '',
        },
        rules: {},
        distributorInfoList: [],
        distributorInfoShow: [],
      }
    },
    mounted() {
      this.getDistributorInfo();
    },
    methods: {
      getDistributorInfo() {
        let that = this;
        getDistributorInfo().then(async res => {
          let data = res.distributorInfoList;
          console.log("data", res.distributorInfoList)
          if (data) {
            that.distributorInfoList = res.distributorInfoList
            // that.distributorInfoShow = res.distributorInfoList;
          }
        }).catch(res => {
          return this.$message({
            message: res.msg,
            type: 'error'
          });
        })
      },
      cancel() {
        this.resetForm()
      },
      doSubmit() {
        this.loading = true
        if (this.isAdd) {
          this.doAdd()
        } else this.doEdit()
      },
      doAdd() {
        add(this.form).then(res => {
          this.resetForm()
          this.$notify({
            title: '添加成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.init()
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
      },
      doEdit() {
        addDistributor(this.form).then(res => {
          this.resetForm()
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.init()
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
      },
      resetForm() {
        this.dialog = false
        this.$refs['form'].resetFields()
        this.form = {
          uid: '',
          account: '',
          pwd: '',
          realName: '',
          birthday: '',
          cardId: '',
          mark: '',
          partnerId: '',
          groupId: '',
          nickname: '',
          avatar: '',
          phone: '',
          addTime: '',
          addIp: '',
          lastTime: '',
          lastIp: '',
          nowMoney: '',
          brokeragePrice: '',
          integral: '',
          signNum: '',
          status: '',
          level: '',
          spreadUid: '',
          spreadTime: '',
          userType: '',
          isPromoter: '',
          payCount: '',
          spreadCount: '',
          cleanTime: '',
          addres: '',
          adminid: '',
          loginType: ''
        }
      },
      queryDistributorSearchAsync(queryString, cb) {
        //在这里为这个数组中每一个对象加一个value字段, 因为autocomplete只识别value字段并在下拉列中显示，所以我从新封装了
        let distributorInfoList = this.distributorInfoList;

        let programList = [];
        for (let i = 0; i < this.distributorInfoList.length; i++) {
          programList.push({'value': distributorInfoList[i].distributorId + '--' + distributorInfoList[i].distributorName})
        }
        let results = queryString ? programList.filter(this.createStateFilter(queryString)) : programList;
        //基本思路就是实现一个过滤器，过滤存在你输入字段的所有数据。
        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          cb(results);
        }, 1000 * Math.random());
      }, handleDistributorSelect(item) {
        var strs = new Array();
        // console.log("item:"+item.value.distributorId)
        strs = item.value.split("--")
        this.form.distributorId = strs[0]
        this.form.distributorName = strs[1]
      },
      createStateFilter(queryString) {
        return (product) => {
          return (product.value.toLowerCase().indexOf(queryString.toLowerCase()) !== -1);
        };
      }
    }
  }
</script>

<style scoped>

</style>
